namespace SubSonic
{
    using SubSonic.Utilities;
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.Common;
    using System.Data.OracleClient;
    using System.Text;

    public class OracleDataProvider : DataProvider
    {
        private const string COLUMN_NAME_PARAMETER = ":columnName";
        private const string DELIMITER = "|";
        private const string END_PARAMETER = ":end";
        private const string GET_FOREIGN_KEY_SQL = "SELECT d.table_name   FROM user_cons_columns c, user_cons_columns d, user_constraints e  WHERE d.constraint_name = e.r_constraint_name    AND c.constraint_name = e.constraint_name    AND d.column_name = :columnName    AND e.table_name = :tableName ";
        private const string GET_PRIMARY_KEY_SQL = "SELECT e.table_name AS TableName, c.column_name AS ColumnName   FROM user_cons_columns c, user_cons_columns d, user_constraints e  WHERE d.constraint_name = e.r_constraint_name    AND c.constraint_name = e.constraint_name    AND d.table_name = :tableName ";
        private const string GET_TABLE_SQL = "SELECT b.table_name   FROM user_constraints a, user_cons_columns b  WHERE a.constraint_name = b.constraint_name    AND a.constraint_type IN ('R', 'P')    AND b.column_name = :columnName    AND a.constraint_type = 'P' ";
        private const string INDEX_SQL = "SELECT b.table_name, b.column_name,        DECODE (a.constraint_type,                'R', 'FOREIGN KEY',                'P', 'PRIMARY KEY',                'UNKNOWN'               ) constraint_type   FROM user_constraints a, user_cons_columns b  WHERE a.constraint_name = b.constraint_name    AND a.constraint_type IN ('R', 'P')    AND b.table_name = :tableName ";
        private const string MANY_TO_MANY_LIST = "SELECT b.table_name FROM user_constraints a, user_cons_columns b WHERE a.table_name = :tableName AND a.constraint_type = 'R' AND a.r_constraint_name = b.constraint_name AND b.table_name like '%' + :mapSuffix";
        private const string MAP_SUFFIX = ":mapSuffix";
        private const string OBJECT_NAME_PARAMETER = ":objectName";
        private const string SP_PARAM_SQL = "SELECT a.object_name, a.object_type, b.position, b.in_out, \r\n                                    b.argument_name, b.data_type, b.char_length, b.data_precision, b.data_scale \r\n                                    FROM user_objects a, user_arguments b \r\n                                    WHERE a.object_type IN ('PROCEDURE', 'FUNCTION', 'PACKAGE') \r\n                                    AND a.object_id = b.object_id \r\n                                    AND a.object_name = :objectName";
        private const string SP_SQL = "SELECT a.object_name, a.object_type, a.created, a.last_ddl_time \r\n    `                           FROM user_objects a \r\n                                WHERE a.object_type IN ('PROCEDURE', 'FUNCTION', 'PACKAGE') ";
        private const string START_PARAMETER = ":start";
        private const string TABLE_COLUMN_SQL = "SELECT user, a.table_name, a.column_name, a.column_id, a.data_default,        a.nullable, a.data_type, a.char_length, a.data_precision, a.data_scale   FROM user_tab_columns a  WHERE a.table_name = :tableName";
        private const string TABLE_NAME_PARAMETER = ":tableName";
        private const string TABLE_SQL = "SELECT a.table_name AS Name FROM user_tables a";
        private const string VIEW_SQL = "SELECT a.view_name AS Name FROM user_views a";

        private static void AddParams(OracleCommand cmd, QueryCommand qry)
        {
            if (qry.Parameters != null)
            {
                foreach (QueryParameter parameter in qry.Parameters)
                {
                    OracleParameter parameter2 = new OracleParameter();
                    parameter2.DbType = parameter.DataType;
                    parameter2.OracleType = GetOracleType(parameter.DataType);
                    parameter2.ParameterName = parameter.ParameterName;
                    parameter2.Value = parameter.ParameterValue;
                    cmd.Parameters.Add(parameter2);
                }
            }
        }

        protected override string AdjustUpdateSql(Query qry, TableSchema.Table table, string updateSql)
        {
            return updateSql;
        }

        public override QueryCommand BuildCommand(Query qry)
        {
            QueryCommand command = null;
            switch (qry.QueryType)
            {
                case QueryType.Select:
                    command = base.BuildSelectCommand(qry);
                    break;

                case QueryType.Update:
                    command = base.BuildUpdateCommand(qry);
                    break;

                case QueryType.Insert:
                    command = null;
                    break;

                case QueryType.Delete:
                    command = base.BuildDeleteCommand(qry);
                    break;
            }
            if (command != null)
            {
                command.ProviderName = this.Name;
            }
            return command;
        }

        internal override DbConnection CreateConnection()
        {
            return this.CreateConnection(base.DefaultConnectionString);
        }

        internal override DbConnection CreateConnection(string newConnectionString)
        {
            OracleConnection connection = new OracleConnection(newConnectionString);
            connection.Open();
            return connection;
        }

        public override int ExecuteQuery(QueryCommand qry)
        {
            using (AutomaticConnectionScope scope = new AutomaticConnectionScope(this))
            {
                OracleCommand cmd = new OracleCommand(qry.CommandSql);
                cmd.CommandType = qry.CommandType;
                cmd.CommandTimeout = qry.CommandTimeout;
                AddParams(cmd, qry);
                cmd.Connection = (OracleConnection) scope.Connection;
                return cmd.ExecuteNonQuery();
            }
        }

        public override object ExecuteScalar(QueryCommand qry)
        {
            using (AutomaticConnectionScope scope = new AutomaticConnectionScope(this))
            {
                OracleCommand cmd = new OracleCommand(qry.CommandSql);
                cmd.CommandType = qry.CommandType;
                cmd.CommandTimeout = qry.CommandTimeout;
                AddParams(cmd, qry);
                cmd.Connection = (OracleConnection) scope.Connection;
                return cmd.ExecuteScalar();
            }
        }

        public override void ExecuteTransaction(QueryCommandCollection commands)
        {
            if (commands.Count > 0)
            {
                OracleCommand cmd = null;
                using (AutomaticConnectionScope scope = new AutomaticConnectionScope(this))
                {
                    if (scope.Connection.State == ConnectionState.Closed)
                    {
                        scope.Connection.Open();
                    }
                    OracleTransaction transaction = (OracleTransaction) scope.Connection.BeginTransaction();
                    foreach (QueryCommand command2 in commands)
                    {
                        cmd = new OracleCommand(command2.CommandSql, (OracleConnection) scope.Connection);
                        cmd.CommandType = command2.CommandType;
                        AddParams(cmd, command2);
                        try
                        {
                            cmd.ExecuteNonQuery();
                            continue;
                        }
                        catch (OracleException exception)
                        {
                            transaction.Rollback();
                            scope.Connection.Close();
                            cmd.Dispose();
                            throw new Exception(exception.Message);
                        }
                    }
                    transaction.Commit();
                    scope.Connection.Close();
                    if (cmd != null)
                    {
                        cmd.Dispose();
                    }
                    return;
                }
            }
            throw new Exception("No commands present");
        }

        public override IDbCommand GetCommand(QueryCommand qry)
        {
            OracleCommand cmd = new OracleCommand(qry.CommandSql);
            AddParams(cmd, qry);
            return cmd;
        }

        internal override string GetDatabaseVersion(string providerName)
        {
            return "Unknown";
        }

        public override T GetDataSet<T>(QueryCommand qry) //where T: DataSet, new()
        {
            T dataSet = Activator.CreateInstance<T>();
            OracleCommand selectCommand = new OracleCommand(qry.CommandSql);
            selectCommand.CommandType = qry.CommandType;
            selectCommand.CommandTimeout = qry.CommandTimeout;
            OracleDataAdapter adapter = new OracleDataAdapter(selectCommand);
            using (AutomaticConnectionScope scope = new AutomaticConnectionScope(this))
            {
                selectCommand.Connection = (OracleConnection) scope.Connection;
                AddParams(selectCommand, qry);
                adapter.Fill(dataSet);
                selectCommand.Dispose();
                adapter.Dispose();
                return dataSet;
            }
        }

        public override DbCommand GetDbCommand(QueryCommand qry)
        {
            AutomaticConnectionScope scope = new AutomaticConnectionScope(this);
            DbCommand command = scope.Connection.CreateCommand();
            command.CommandText = qry.CommandSql;
            command.CommandType = qry.CommandType;
            foreach (QueryParameter parameter in qry.Parameters)
            {
                command.Parameters.Add(parameter);
            }
            return command;
        }

        public override DbType GetDbType(string sqlType)
        {
            switch (sqlType)
            {
                case "number":
                    return DbType.Single;

                case "float":
                    return DbType.Double;

                case "varchar2":
                case "varchar":
                case "char":
                case "nchar":
                case "nvarchar2":
                case "rowid":
                case "nclob":
                case "blob":
                    return DbType.String;

                case "raw":
                case "long raw":
                    return DbType.Binary;

                case "date":
                case "timestamp":
                    return DbType.DateTime;
            }
            return DbType.String;
        }

        public static DbType GetDbTypeOracle(string sqlType, int dataScale, int dataPrecision)
        {
            switch (sqlType)
            {
                case "varchar2":
                case "varchar":
                case "char":
                case "nchar":
                case "nvarchar2":
                case "rowid":
                    return DbType.String;

                case "nclob":
                case "clob":
                    return DbType.AnsiString;

                case "number":
                    return DbType.Decimal;

                case "float":
                    return DbType.Double;

                case "raw":
                case "long raw":
                case "blob":
                    return DbType.Binary;

                case "date":
                case "timestamp":
                    return DbType.DateTime;
            }
            return DbType.String;
        }

        public override string GetForeignKeyTableName(string fkColumnName)
        {
            QueryCommand cmd = new QueryCommand("SELECT b.table_name   FROM user_constraints a, user_cons_columns b  WHERE a.constraint_name = b.constraint_name    AND a.constraint_type IN ('R', 'P')    AND b.column_name = :columnName    AND a.constraint_type = 'P' ", this.Name);
            cmd.AddParameter(":columnName", fkColumnName);
            object obj2 = DataService.ExecuteScalar(cmd);
            if (obj2 == null)
            {
                return null;
            }
            return obj2.ToString();
        }

        public override string GetForeignKeyTableName(string fkColumnName, string tableName)
        {
            QueryCommand cmd = new QueryCommand("SELECT d.table_name   FROM user_cons_columns c, user_cons_columns d, user_constraints e  WHERE d.constraint_name = e.r_constraint_name    AND c.constraint_name = e.constraint_name    AND d.column_name = :columnName    AND e.table_name = :tableName ", this.Name);
            cmd.AddParameter(":columnName", fkColumnName);
            cmd.AddParameter(":tableName", tableName);
            object obj2 = DataService.ExecuteScalar(cmd);
            if (obj2 == null)
            {
                return null;
            }
            return obj2.ToString();
        }

        public override string[] GetForeignKeyTables(string tableName)
        {
            return new string[] { "" };
        }

        public override string GetInsertSql(Query qry)
        {
            TableSchema.Table schema = qry.Schema;
            StringBuilder builder = new StringBuilder();
            builder.Append("INSERT INTO ");
            builder.Append(schema.Name);
            StringBuilder builder2 = new StringBuilder();
            StringBuilder builder3 = new StringBuilder();
            bool flag = true;
            foreach (TableSchema.TableColumn column in schema.Columns)
            {
                if (!column.AutoIncrement && !column.IsReadOnly)
                {
                    if (!flag)
                    {
                        builder2.Append(",");
                        builder3.Append(",");
                    }
                    flag = false;
                    builder2.Append(column.ColumnName);
                    builder3.Append(this.MakeParam(column.ColumnName));
                }
            }
            builder.Append("(");
            builder.Append(builder2);
            builder.Append(") ");
            builder.Append("VALUES(");
            builder.Append(builder3);
            builder.Append(")");
            return builder.ToString();
        }

        public static OracleType GetOracleType(DbType dbType)
        {
            switch (dbType)
            {
                case DbType.AnsiString:
                    return OracleType.VarChar;

                case DbType.Binary:
                    return OracleType.Raw;

                case DbType.Byte:
                    return OracleType.Byte;

                case DbType.Boolean:
                    return OracleType.Byte;

                case DbType.Currency:
                    return OracleType.Number;

                case DbType.Date:
                    return OracleType.DateTime;

                case DbType.DateTime:
                    return OracleType.DateTime;

                case DbType.Decimal:
                    return OracleType.Number;

                case DbType.Double:
                    return OracleType.Double;

                case DbType.Guid:
                    return OracleType.Raw;

                case DbType.Int16:
                    return OracleType.Int16;

                case DbType.Int32:
                    return OracleType.Int32;

                case DbType.Int64:
                    return OracleType.Number;

                case DbType.Object:
                    return OracleType.Blob;

                case DbType.SByte:
                    return OracleType.SByte;

                case DbType.Single:
                    return OracleType.Float;

                case DbType.String:
                    return OracleType.NVarChar;

                case DbType.Time:
                    return OracleType.DateTime;

                case DbType.UInt16:
                    return OracleType.UInt16;

                case DbType.UInt32:
                    return OracleType.UInt32;

                case DbType.UInt64:
                    return OracleType.Number;

                case DbType.VarNumeric:
                    return OracleType.Number;

                case DbType.AnsiStringFixedLength:
                    return OracleType.Char;

                case DbType.StringFixedLength:
                    return OracleType.NChar;
            }
            return OracleType.VarChar;
        }

        public override string GetParameterPrefix()
        {
            return ":";
        }

        public override ArrayList GetPrimaryKeyTableNames(string tableName)
        {
            QueryCommand cmd = new QueryCommand("SELECT e.table_name AS TableName, c.column_name AS ColumnName   FROM user_cons_columns c, user_cons_columns d, user_constraints e  WHERE d.constraint_name = e.r_constraint_name    AND c.constraint_name = e.constraint_name    AND d.table_name = :tableName ", this.Name);
            cmd.AddParameter(":tableName", tableName);
            ArrayList list = new ArrayList();
            using (IDataReader reader = DataService.GetReader(cmd))
            {
                while (reader.Read())
                {
                    list.Add(new string[] { reader["TableName"].ToString(), reader["ColumnName"].ToString() });
                }
                reader.Close();
            }
            return list;
        }

        public override TableSchema.Table[] GetPrimaryKeyTables(string tableName)
        {
            QueryCommand cmd = new QueryCommand("SELECT e.table_name AS TableName, c.column_name AS ColumnName   FROM user_cons_columns c, user_cons_columns d, user_constraints e  WHERE d.constraint_name = e.r_constraint_name    AND c.constraint_name = e.constraint_name    AND d.table_name = :tableName ", this.Name);
            cmd.AddParameter(":tableName", tableName);
            ArrayList list = new ArrayList();
            using (IDataReader reader = this.GetReader(cmd))
            {
                while (reader.Read())
                {
                    list.Add(reader["TableName"].ToString());
                }
                reader.Close();
            }
            if (list.Count <= 0)
            {
                return null;
            }
            TableSchema.Table[] tableArray = new TableSchema.Table[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                tableArray[i] = DataService.GetSchema((string) list[i], this.Name, TableType.Table);
            }
            return tableArray;
        }

        protected static string GetQualifiedSelect(TableSchema.Table table)
        {
            StringBuilder builder = new StringBuilder();
            bool flag = true;
            foreach (TableSchema.TableColumn column in table.Columns)
            {
                if (!flag)
                {
                    builder.Append(", ");
                }
                flag = false;
                builder.AppendFormat("{0}.{1}", table.Name, column.ColumnName);
            }
            return builder.ToString();
        }

        public override IDataReader GetReader(QueryCommand qry)
        {
            IDataReader reader;
            AutomaticConnectionScope scope = new AutomaticConnectionScope(this);
            OracleCommand cmd = new OracleCommand(qry.CommandSql);
            cmd.CommandType = qry.CommandType;
            cmd.CommandTimeout = qry.CommandTimeout;
            AddParams(cmd, qry);
            cmd.Connection = (OracleConnection) scope.Connection;
            try
            {
                reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (OracleException exception)
            {
                scope.Dispose();
                throw exception;
            }
            return reader;
        }

        public override string GetSelectSql(Query qry)
        {
            string selectList;
            TableSchema.Table schema = qry.Schema;
            string str = "SELECT ";
            str = str + (qry.IsDistinct ? "DISTINCT " : string.Empty);
            StringBuilder builder = new StringBuilder();
            if (!string.IsNullOrEmpty(qry.SelectList) && (qry.SelectList.Trim().Length >= 2))
            {
                selectList = qry.SelectList;
            }
            else
            {
                selectList = GetQualifiedSelect(schema);
            }
            string str4 = DataProvider.BuildWhere(qry);
            if (qry.OrderByCollection.Count > 0)
            {
                for (int i = 0; i < qry.OrderByCollection.Count; i++)
                {
                    string orderString = qry.OrderByCollection[i].OrderString;
                    if (!string.IsNullOrEmpty(orderString))
                    {
                        builder.Append(orderString);
                        if ((i + 1) != qry.OrderByCollection.Count)
                        {
                            builder.Append(", ");
                        }
                    }
                }
            }
            else if (schema.PrimaryKey != null)
            {
                builder.Append(OrderBy.Asc(schema.PrimaryKey.ColumnName).OrderString);
            }
            if (builder.Length > 0)
            {
                builder = builder.Replace("[", string.Empty).Replace("]", string.Empty);
            }
            if (qry.PageIndex < 0)
            {
                return string.Concat(new object[] { str, selectList, " FROM ", schema.Name, str4, builder });
            }
            int num2 = qry.PageIndex * qry.PageSize;
            int num3 = (qry.PageIndex + 1) * qry.PageSize;
            string format = "with __PagedTable ({0}, __RowIndex) as (Select {1}, Row_Number() OVER ({2}) as '__RowIndex' FROM [{3}] {4})Select {0}, __RowIndex FROM __PagedTable where __RowIndex >= {5} and __RowIndex < {6} Order by __RowIndex";
            return string.Format(format, new object[] { selectList.Replace("[" + schema.Name + "].", string.Empty), selectList, builder, schema.Name, str4, num2, num3 });
        }

        public override string[] GetSPList()
        {
            QueryCommand cmd = new QueryCommand("SELECT a.object_name, a.object_type, a.created, a.last_ddl_time \r\n    `                           FROM user_objects a \r\n                                WHERE a.object_type IN ('PROCEDURE', 'FUNCTION', 'PACKAGE') ", this.Name);
            StringBuilder builder = new StringBuilder();
            using (IDataReader reader = DataService.GetReader(cmd))
            {
                bool flag = true;
                while (reader.Read())
                {
                    if (!flag)
                    {
                        builder.Append("|");
                    }
                    flag = false;
                    builder.Append(reader[0]);
                }
                reader.Close();
                return builder.ToString().Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            }
        }

        public override IDataReader GetSPParams(string spName)
        {
            QueryCommand cmd = new QueryCommand("SELECT a.object_name, a.object_type, b.position, b.in_out, \r\n                                    b.argument_name, b.data_type, b.char_length, b.data_precision, b.data_scale \r\n                                    FROM user_objects a, user_arguments b \r\n                                    WHERE a.object_type IN ('PROCEDURE', 'FUNCTION', 'PACKAGE') \r\n                                    AND a.object_id = b.object_id \r\n                                    AND a.object_name = :objectName", this.Name);
            cmd.AddParameter(":objectName", spName);
            return DataService.GetReader(cmd);
        }

        public override string GetSql(Query qry)
        {
            string str = string.Empty;
            switch (qry.QueryType)
            {
                case QueryType.Select:
                    return this.GetSelectSql(qry);

                case QueryType.Update:
                    return base.GetUpdateSql(qry);

                case QueryType.Insert:
                    return this.GetInsertSql(qry);

                case QueryType.Delete:
                    return DataProvider.GetDeleteSql(qry);
            }
            return str;
        }

        public override string GetTableNameByPrimaryKey(string pkName, string providerName)
        {
            return string.Empty;
        }

        public override string[] GetTableNameList()
        {
            QueryCommand cmd = new QueryCommand("SELECT a.table_name AS Name FROM user_tables a", this.Name);
            StringBuilder builder = new StringBuilder();
            using (IDataReader reader = DataService.GetReader(cmd))
            {
                bool flag = true;
                while (reader.Read())
                {
                    if (!flag)
                    {
                        builder.Append("|");
                    }
                    flag = false;
                    builder.Append(reader["Name"]);
                }
                reader.Close();
                string[] array = builder.ToString().Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                Array.Sort<string>(array);
                return array;
            }
        }

        public override TableSchema.Table GetTableSchema(string tableName, TableType tableType)
        {
            TableSchema.TableColumn column;
            TableSchema.TableColumnCollection columns = new TableSchema.TableColumnCollection();
            TableSchema.Table tableSchema = new TableSchema.Table(tableName, tableType, this);
            string sql = "SELECT user, a.table_name, a.column_name, a.column_id, a.data_default,        a.nullable, a.data_type, a.char_length, a.data_precision, a.data_scale   FROM user_tab_columns a  WHERE a.table_name = :tableName";
            QueryCommand cmd = new QueryCommand(sql, this.Name);
            cmd.AddParameter(":tableName", tableName);
            using (IDataReader reader = DataService.GetReader(cmd))
            {
                while (reader.Read())
                {
                    int num;
                    column = new TableSchema.TableColumn(tableSchema);
                    column.ColumnName = reader["COLUMN_NAME"].ToString();
                    string str2 = reader["DATA_SCALE"].ToString();
                    string str3 = reader["DATA_PRECISION"].ToString();
                    column.NumberScale = 0;
                    column.NumberPrecision = 0;
                    if (!string.IsNullOrEmpty(str2) && (str2 != "0"))
                    {
                        column.NumberScale = int.Parse(str2);
                    }
                    if (!string.IsNullOrEmpty(str3) && (str3 != "0"))
                    {
                        column.NumberPrecision = int.Parse(str3);
                    }
                    column.DataType = GetDbTypeOracle(reader["DATA_TYPE"].ToString().ToLower(), column.NumberScale, column.NumberPrecision);
                    column.AutoIncrement = false;
                    int.TryParse(reader["CHAR_LENGTH"].ToString(), out num);
                    column.MaxLength = num;
                    column.IsNullable = Utility.IsMatch(reader["NULLABLE"].ToString(), "Y");
                    column.IsReadOnly = false;
                    columns.Add(column);
                }
            }
            cmd.CommandSql = "SELECT b.table_name, b.column_name,        DECODE (a.constraint_type,                'R', 'FOREIGN KEY',                'P', 'PRIMARY KEY',                'UNKNOWN'               ) constraint_type   FROM user_constraints a, user_cons_columns b  WHERE a.constraint_name = b.constraint_name    AND a.constraint_type IN ('R', 'P')    AND b.table_name = :tableName ";
            using (IDataReader reader2 = DataService.GetReader(cmd))
            {
                while (reader2.Read())
                {
                    string columnName = reader2["COLUMN_NAME"].ToString();
                    string str6 = reader2["CONSTRAINT_TYPE"].ToString();
                    column = columns.GetColumn(columnName);
                    switch (str6)
                    {
                        case "PRIMARY KEY":
                            column.IsPrimaryKey = true;
                            break;

                        case "FOREIGN KEY":
                            column.IsForeignKey = true;
                            break;
                    }
                    column.ColumnName = column.ColumnName;
                }
                reader2.Close();
            }
            tableSchema.Columns = columns;
            return tableSchema;
        }

        public override string[] GetViewNameList()
        {
            QueryCommand cmd = new QueryCommand("SELECT a.view_name AS Name FROM user_views a", this.Name);
            StringBuilder builder = new StringBuilder();
            using (IDataReader reader = DataService.GetReader(cmd))
            {
                while (reader.Read())
                {
                    string str = reader["Name"].ToString();
                    if (string.IsNullOrEmpty(base.ViewStartsWith) || str.StartsWith(base.ViewStartsWith))
                    {
                        builder.Append(reader["Name"]);
                        builder.Append("|");
                    }
                }
                reader.Close();
                string[] array = builder.ToString().Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                Array.Sort<string>(array);
                return array;
            }
        }

        public override string ScriptData(string tableName)
        {
            return this.ScriptData(tableName, DataService.Provider.Name);
        }

        public override string ScriptData(string tableName, string providerName)
        {
            StringBuilder builder = new StringBuilder();
            StringBuilder builder2 = new StringBuilder();
            StringBuilder builder3 = new StringBuilder();
            StringBuilder builder4 = new StringBuilder();
            builder4.Append("INSERT INTO ");
            builder4.Append(tableName);
            builder4.Append(" ");
            TableSchema.Table tbl = Query.BuildTableSchema(tableName, providerName);
            bool flag = true;
            foreach (TableSchema.TableColumn column in tbl.Columns)
            {
                if (!flag)
                {
                    builder.Append(",");
                }
                flag = false;
                builder.Append(column.ColumnName);
            }
            builder4.Append("(");
            builder4.Append(builder.ToString());
            builder4.AppendLine(")");
            IDataReader reader = new Query(tbl).ExecuteReader();
            while (reader.Read())
            {
                StringBuilder builder5 = new StringBuilder();
                builder5.Append(builder4);
                builder5.Append("VALUES(");
                flag = true;
                foreach (TableSchema.TableColumn column2 in tbl.Columns)
                {
                    if (!flag)
                    {
                        builder5.Append(",");
                    }
                    flag = false;
                    if (Utility.IsNumeric(column2))
                    {
                        builder5.Append(reader[column2.ColumnName]);
                    }
                    else
                    {
                        builder5.Append("'");
                        builder5.Append(reader[column2.ColumnName].ToString().Replace("'", "''"));
                    }
                }
                builder5.AppendLine(")");
                builder2.Append(builder5.ToString());
            }
            reader.Close();
            builder3.Append("PRINT 'Begin inserting data in ");
            builder3.Append(tableName);
            builder3.AppendLine("'");
            builder3.Append(builder2.ToString());
            return builder3.ToString();
        }

        public override string ScriptSchema()
        {
            string str = "";
            return (str + "\r\n\r\n");
        }

        public override void SetParameter(IDataReader rdr, StoredProcedure.Parameter par)
        {
            par.DBType = this.GetDbType(rdr["DATA_TYPE"].ToString());
            if (rdr["IN_OUT"].ToString() == "IN/OUT")
            {
                par.Mode = ParameterDirection.InputOutput;
            }
            par.Name = rdr["ARGUMENT_NAME"].ToString();
        }
    }
}

